package com.itheima.student.service.Impl;

import com.itheima.domain.Student;
import com.itheima.factory.Factory;
import com.itheima.student.dao.IStudentDao;
import com.itheima.student.dao.Impl.StudentDaoImpl;
import com.itheima.student.service.IStudentService;

import java.io.*;
import java.util.ArrayList;

public class StudentServiceImpl implements IStudentService {

    //调用工厂类
    private IStudentDao studentDao;

    {
        try {
            studentDao = Factory.getIStudentDao();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    增
    */
    @Override
    public void addStudent(Student student) {
        studentDao.addStudent(student);
    }


    /*
    删
    */
    //指定学号删除
    @Override
    public void deleteSidStudent(String sid) {
        studentDao.deleteSidStudent(sid);
    }

    //删除全部学生
    @Override
    public void deleteAllStudent() {
        studentDao.deleteAllStudent();
    }


    /*
    改
    */
    @Override
    public void updateStudent(String sid, Student student) {
        studentDao.updateStudent(sid, student);
    }

    //查询全部
    @Override
    public ArrayList<Student> findAllStudent() {
        return studentDao.findAllStudent();
    }


    /*
    查
    */
    //按学号查
    @Override
    public Student findSidStudent(String sid) {
        return studentDao.findSidStudent(sid);
    }

    //按姓名查
    @Override
    public ArrayList<Student> findNameStudent(String name) {
        return studentDao.findNameStudent(name);
    }

    //按年龄查
    @Override
    public ArrayList<Student> findAgeStudent(int age) {
        return studentDao.findAgeStudent(age);
    }

    //按手机号查
    @Override
    public ArrayList<Student> findPhoneStudent(String phone) {
        return studentDao.findPhoneStudent(phone);
    }

    //按组号查
    @Override
    public ArrayList<Student> findGroupStudent(int group) {
        return studentDao.findGroupStudent(group);
    }


    /*
    判断
    */
    //判断学号是否存在
    @Override
    public boolean isExists(String sid) {
        ArrayList<Student> students = studentDao.findAllStudent();
        for (Student student : students) {
            if (sid.equals(student.getSid())) {
                return true;
            }
        }
        return false;
    }


    //手机号是否存在
    @Override
    public boolean phoneIsExists(String phone) {
        ArrayList<Student> students = studentDao.findAllStudent();
        for (Student student : students) {
            if (phone.equals(student.getPhone())) {
                return true;
            }
        }
        return false;
    }

    //判断手机号格式,
    @Override
    public boolean phoneIsTrue(String phone) {
        char[] chars = phone.toCharArray();
        int count = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '0' || chars[i] == '1' || chars[i] == '2' || chars[i] == '3' || chars[i] == '4' || chars[i] == '5' || chars[i] == '6' || chars[i] == '7' || chars[i] == '8' || chars[i] == '9') {
                count++;
            }
        }
        return count == chars.length && chars.length == 11;//true或者false
    }

    //判断性别格式
    @Override
    public boolean sexIsTrue(String sex) {
        if (sex.equals("男") || sex.equals("女")) {
            return true;
        }
        return false;
    }


    //写
    @Override
    public void writeStudent() throws IOException {
        studentDao.writeStudent();
    }

    //读
    @Override
    public void readStudent() throws Exception {
        studentDao.readStudent();
    }
}
